# coding=utf-8


import os
from datetime import datetime
from typing import List
import xlrd

from generator.types_hal.property import Property, TypesHal
from generator.types_hal.types_hal import gen_types_hal
from utils.payload import EncodeType
from parse.csv2xlsx import delxlsx, dealFile

BASE_ID_NUM = 1030

class TypesHalPaser:
    def __init__(self,dir:str) -> None:
        self.typesHal=TypesHal()
        self.directTransmitList:List[str]=[]

    def parse(self)->None:
        exclePath=os.path.abspath('config/vehicleProp/property.xlsx')
        dealFile(exclePath) #假如有csv_dir则替换xlsx， 兼容没有csv_dir场景
        if not os.path.exists(exclePath):
            print("not found property.xlsx file pass:"+exclePath)
            return
        excle: xlrd.book.Book = xlrd.open_workbook(exclePath)
        propSheet=excle.sheet_by_name("vhalProperties")
        adasSheet=excle.sheet_by_name("ADAS")
        chargingSheet=excle.sheet_by_name("CHARGING")
        hvacSheet=excle.sheet_by_name("HVAC")
        drivemodeSheet=excle.sheet_by_name("DRIVEMODE")
        audioSheet=excle.sheet_by_name("AUDIO")
        dvrSheet=excle.sheet_by_name("DVR")
        hudSheet=excle.sheet_by_name("HUD")
        seatSheet=excle.sheet_by_name("SEAT")
        doorSheet=excle.sheet_by_name("DOOR")
        windowSheet=excle.sheet_by_name("WINDOW")
        lightSheet=excle.sheet_by_name("LIGHT")
        wpcSheet=excle.sheet_by_name("WPC")
        mirrorSheet=excle.sheet_by_name("MIRROR")
        wheelSheet=excle.sheet_by_name("WHEEL")
        energySheet=excle.sheet_by_name("ENERGY")
        tripSheet=excle.sheet_by_name("TRIP")
        vehicleSheet=excle.sheet_by_name("VEHICLE")
        dimSheet=excle.sheet_by_name("DIM")
        
        propLists = []
        propLists.append(propSheet)
        propLists.append(adasSheet)
        propLists.append(chargingSheet)
        propLists.append(hvacSheet)
        propLists.append(drivemodeSheet)
        propLists.append(audioSheet)
        propLists.append(dvrSheet)
        propLists.append(hudSheet)
        propLists.append(seatSheet)
        propLists.append(doorSheet)
        propLists.append(windowSheet)
        propLists.append(lightSheet)
        propLists.append(wpcSheet)
        propLists.append(mirrorSheet)
        propLists.append(wheelSheet)
        propLists.append(energySheet)
        propLists.append(tripSheet)
        propLists.append(vehicleSheet)
        propLists.append(dimSheet)
        
        enumSheet=excle.sheet_by_name("CustomEnum")
        enums:List[EncodeType]=[]
        for rowIndex in range(1,enumSheet.nrows):
            enumName=enumSheet.cell_value(rowIndex,0)
            enumMember=enumSheet.cell_value(rowIndex,1)
            (name,value)=enumMember.split("=")
            memberDesc=enumSheet.cell_value(rowIndex,2)
            if len(enums) == 0:
                encodeType=EncodeType(enumName,isEnum=True)
                encodeType.addMember("{}={}".format(name,value),memberDesc)
                enums.append(encodeType)
            else:
                encodeType=enums[len(enums)-1]
                if encodeType.getTypeName().lower() == enumName.lower():
                    encodeType.addMember("{}={}".format(name,value),memberDesc)
                else:
                    encodeType=EncodeType(enumName,isEnum=True)
                    encodeType.addMember("{}={}".format(name,value),memberDesc)
                    enums.append(encodeType)
        self.typesHal.enums.extend(enums)

        for sheet in propLists:
            for rowIndex in range(1,sheet.nrows):
                propName=sheet.cell_value(rowIndex,0)
                prop=Property(propName)
                if sheet == propSheet:
                    if rowIndex > BASE_ID_NUM:
                        break
                    changeMode=sheet.cell_value(rowIndex,5)
                    accessMode=sheet.cell_value(rowIndex,6)
                    propType=sheet.cell_value(rowIndex,7)
                    propArea=sheet.cell_value(rowIndex,8)
                    enumName=sheet.cell_value(rowIndex,9)
                    desc=sheet.cell_value(rowIndex,10)
                else:
                    module=sheet.cell_value(rowIndex,1)
                    changeMode=sheet.cell_value(rowIndex,2)
                    accessMode=sheet.cell_value(rowIndex,3)
                    propType=sheet.cell_value(rowIndex,4)
                    propArea=sheet.cell_value(rowIndex,5)
                    enumName=sheet.cell_value(rowIndex,6)
                    swraID=sheet.cell_value(rowIndex,7)
                    author=sheet.cell_value(rowIndex,8)
                    add_date=sheet.cell_value(rowIndex,9)
                    desc=sheet.cell_value(rowIndex,10)
                    internalProp=sheet.cell_value(rowIndex,11)
                    prop.setPropModule(module)
                    prop.setPropSwraid(swraID)
                    prop.setPropAuthor(author)
                    prop.setPropDate(add_date)
                    prop.setPropInternal(internalProp)
                prop.setChangeMode(changeMode)
                prop.setAccessMode(accessMode)
                prop.setPropArea(propArea)
                prop.setPropType(propType)
                prop.setDesc(desc)
                prop.setEncodeType(enumName)

                if sheet == propSheet:
                    self.typesHal.addProperty(prop)
                elif sheet == adasSheet:
                    self.typesHal.addAdasProperty(prop)
                elif sheet == chargingSheet:
                    self.typesHal.addChargingProperty(prop)
                elif sheet == hvacSheet:
                    self.typesHal.addHvacProperty(prop)
                elif sheet == drivemodeSheet:
                    self.typesHal.addDrivemodeProperty(prop)
                elif sheet == audioSheet:
                    self.typesHal.addAudioProperty(prop)
                elif sheet == dvrSheet:
                    self.typesHal.addDvrProperty(prop)
                elif sheet == hudSheet:
                    self.typesHal.addHudProperty(prop)
                elif sheet == seatSheet:
                    self.typesHal.addSeatProperty(prop)
                elif sheet == doorSheet:
                    self.typesHal.addDoorProperty(prop)
                elif sheet == windowSheet:
                    self.typesHal.addWindowProperty(prop)
                elif sheet == lightSheet:
                    self.typesHal.addLightProperty(prop)
                elif sheet == wpcSheet:
                    self.typesHal.addWpcProperty(prop)
                elif sheet == mirrorSheet:
                    self.typesHal.addMirrorProperty(prop)
                elif sheet == wheelSheet:
                    self.typesHal.addWheelProperty(prop)
                elif sheet == energySheet:
                    self.typesHal.addEnergyProperty(prop)
                elif sheet == tripSheet:
                    self.typesHal.addTripProperty(prop)
                elif sheet == vehicleSheet:
                    self.typesHal.addVehicleProperty(prop)
                elif sheet == dimSheet:
                    self.typesHal.addDimProperty(prop)

        self.typesHal.propList.append(self.typesHal.adasProps)
        self.typesHal.propList.append(self.typesHal.chargingProps)
        self.typesHal.propList.append(self.typesHal.hvacProps)
        self.typesHal.propList.append(self.typesHal.drivemodeProps)
        self.typesHal.propList.append(self.typesHal.audioProps)
        self.typesHal.propList.append(self.typesHal.dvrProps)
        self.typesHal.propList.append(self.typesHal.hudProps)
        self.typesHal.propList.append(self.typesHal.seatProps)
        self.typesHal.propList.append(self.typesHal.doorProps)
        self.typesHal.propList.append(self.typesHal.windowProps)
        self.typesHal.propList.append(self.typesHal.lightProps)
        self.typesHal.propList.append(self.typesHal.wpcProps)
        self.typesHal.propList.append(self.typesHal.mirrorProps)
        self.typesHal.propList.append(self.typesHal.wheelProps)
        self.typesHal.propList.append(self.typesHal.energyProps)
        self.typesHal.propList.append(self.typesHal.tripProps)
        self.typesHal.propList.append(self.typesHal.vehicleProps)
        self.typesHal.propList.append(self.typesHal.dimProps)
        
        #读取需要透传的信号
        print("zpy deal passthrough")
        passthroughSheet=excle.sheet_by_name("passthrough")
        for rowIndex in range(1,passthroughSheet.nrows):
            string = str(passthroughSheet.cell_value(rowIndex,0)).strip()
            # print("rowIndex:,value", rowIndex, string)
            self.directTransmitList.append(str(passthroughSheet.cell_value(rowIndex,0)).strip())
        print("zpy deal passthrough end")
        delxlsx(exclePath) #假如存在csv_dir 删除xlsx，没有则什么也不做

    def genertor_file(self):
        gen_types_hal(self.typesHal)

